package com.fuda.trade.model
import org.junit.jupiter.api.Test

import org.junit.jupiter.api.Assertions

class TradeHoldingUnitTest {
    @Test
    fun deserializeListIsCorrect() {
        val jsonString = """
           [{"id":{"tb":"trade_holding","id":{"String":"MY.HOLDING.MONEY.CNY"}},"holding_id":"MY.HOLDING.MONEY.CNY","stop_loss_price":null,"take_profit_price":null,"data":{"type":"com.fuda.trade.model.TradeHoldingData.Money","currency":"CNY","total":1000.0,"available":200.0,"asset_total":500.0,"toady_profit":300.0,"holding_profit":600.0},"last_updated":"7405812557377776464/30201"},
           {"id":{"tb":"trade_holding","id":{"String":"SH.STOCK.600000"}},"holding_id":"SH.STOCK.600000","stop_loss_price":null,"take_profit_price":null,"data":{"type":"com.fuda.trade.model.TradeHoldingData.Share","security_name":"贵州茅台0","profit_loss_amount":123.0,"holding_percentage":18.0,"profit_loss_percentage":123.0,"market_value":123.0,"current_price":123.0,"cost_basis":123.0,"shares_held":123,"available_shares":123},"last_updated":"7405812557468902800/30201"},
           {"id":{"tb":"trade_holding","id":{"String":"SH.STOCK.600001"}},"holding_id":"SH.STOCK.600001","stop_loss_price":null,"take_profit_price":null,"data":{"type":"com.fuda.trade.model.TradeHoldingData.Share","security_name":"贵州茅台1","profit_loss_amount":123.0,"holding_percentage":18.0,"profit_loss_percentage":123.0,"market_value":123.0,"current_price":123.0,"cost_basis":123.0,"shares_held":123,"available_shares":123},"last_updated":"7405812557490244480/30201"},
           {"id":{"tb":"trade_holding","id":{"String":"SH.STOCK.600002"}},"holding_id":"SH.STOCK.600002","stop_loss_price":null,"take_profit_price":null,"data":{"type":"com.fuda.trade.model.TradeHoldingData.Share","security_name":"贵州茅台2","profit_loss_amount":123.0,"holding_percentage":18.0,"profit_loss_percentage":123.0,"market_value":123.0,"current_price":123.0,"cost_basis":123.0,"shares_held":123,"available_shares":123},"last_updated":"7405812557503984080/30201"}
           ]  
"""
            .trimIndent()


        // Parse the JSON array into a list of TradeHolding objects
        val tradeOrders = TradeHolding.deserializeList(jsonString)
        Assertions.assertEquals(tradeOrders.size, 4)
        Assertions.assertEquals( tradeOrders[2].holdingId,"SH.STOCK.600001")

        when (val data:TradeHoldingData? = tradeOrders[2].data){
            is TradeHoldingData.Share -> {
                Assertions.assertEquals(data.sharesHeld,123)

            }
            else -> Assertions.assertEquals(true,false)
        }
    }
}